//
//  TouchMaskView.m
//  FengHui_iPad
//
//  Created by iPhuan on 12-12-16.
//  Copyright (c) 2012年 iTotem. All rights reserved.
//

#import "TouchMaskView.h"

#define SIDEBAR_VIEW_WIDTH      220  

@interface TouchMaskView ()
{
    CGPoint _beganPoint;
}

@end

@implementation TouchMaskView

- (void)dealloc
{
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initGestureRecognizer];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self initGestureRecognizer];
}

- (void)initGestureRecognizer
{
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerAction:)];
    [self addGestureRecognizer:panGestureRecognizer];
    [panGestureRecognizer release];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizerAction:)];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tapGestureRecognizer];
    [tapGestureRecognizer release];
}

- (void)panGestureRecognizerAction:(UIPanGestureRecognizer *)panGestureRecognizer
{    
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        _beganPoint = [panGestureRecognizer locationInView:APP_DELEGATE.window];
    }
    else if (panGestureRecognizer.state == UIGestureRecognizerStateChanged)
    {
        CGPoint changedPoint = [panGestureRecognizer locationInView:APP_DELEGATE.window];
        CGFloat changedDistance = changedPoint.x - _beganPoint.x;
        CGFloat left = APP_DELEGATE.window.rootViewController.view.left + changedDistance;
        if (left > SIDEBAR_VIEW_WIDTH)
        {
            left = SIDEBAR_VIEW_WIDTH;
        }
        if (left < 0)
        {
            left = 0;
        }
        
        [APP_DELEGATE setRootViewLeft:left];
        
        _beganPoint = changedPoint;
    }
    else if (panGestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        CGFloat left = APP_DELEGATE.window.rootViewController.view.left;
        if (left < SIDEBAR_VIEW_WIDTH/2)
        {
            APP_DELEGATE.isSliderViewShowing = NO;
            [UIView animateWithDuration:0.2 animations:^{
                [APP_DELEGATE setRootViewLeft:0];
            }];
            if (_delegate && [_delegate respondsToSelector:@selector(touchMaskViewDidMakeSideBarHidden:)])
            {
                [_delegate touchMaskViewDidMakeSideBarHidden:self];
            }

        }
        else
        {
            APP_DELEGATE.isSliderViewShowing = YES;
            [UIView animateWithDuration:0.2 animations:^{
                [APP_DELEGATE setRootViewLeft:SIDEBAR_VIEW_WIDTH];
            }];

        }
        self.hidden = !APP_DELEGATE.isSliderViewShowing;
    }
}

- (void)tapGestureRecognizerAction:(UITapGestureRecognizer *)tapGestureRecognizer
{
    if (APP_DELEGATE.isSliderViewShowing)
    {
        [APP_DELEGATE showSliderView:NO];
        if (_delegate && [_delegate respondsToSelector:@selector(touchMaskViewDidMakeSideBarHidden:)])
        {
            [_delegate touchMaskViewDidMakeSideBarHidden:self];
        }
    }
}



@end
